<?php
error_reporting(E_ALL);
class indexAction extends Action {
	public function __construct() {
		parent::__construct ();
		//如果没登录，登录
		if (! isset ( $_SESSION ['yxk_member'] )) {
			$this->redirect ( 'login/login' );
		}
		R ( "Admin.mycache", 'getsetting' );
	
	}
	
	public function index() {
		//标题
		$this->assign ( 'title', C ( 'WEB_NAME' ) . "管理页面. 管理员：" . $_SESSION ['yxk_member'] );
		$this->display ();
	}
	//院校列表
	public function listschool() {
		//加入分页类
		import ( "ORG.Util.Page" );
		$listRows = 10; //每页显示的条数
		$setting = S ( "setting" ); //获取系统字段
		$this->assign ( 'syssetting', $setting );
		
		$db = M ( 'school' );
		$count = $db->count ();
		$P = new Page ( $count, $listRows );
		$show = $P->show ();
		$list = $db->order ( 'id desc' )->limit ( $P->firstRow . ',' . $P->listRows )->select ();
		$arr = array ();
		foreach ( $list as $k => $v ) {
			$v ['address'] = $setting [0] [$v ['address']];
			$v ['education'] = $setting [1] [$v ['education']];
			$v ['belonging'] = $setting [2] [$v ['belonging']];
			$v ['schooltype'] = $setting [3] [$v ['schooltype']];
			$v ['typeofinstitution'] = $setting [4] [$v ['typeofinstitution']];
			$v ['attach'] = str_replace ( array ('1', '2', '3' ), array ('985', '211', '研究生院' ), $v ['attach'] );
			$arr [$k] = $v;
		}
		$this->assign ( 'list', $arr );
		$this->assign ( 'page', $show );
		$this->display ();
	}
	
	//添加或修改院校页面
	public function editschool() {
		$setting = S ( 'setting' );
		$this->assign ( 'syssetting', $setting );
		$this->assign ( 'title', C ( "WEB_NAME" ) . "添加院校信息" );
		if (isset ( $_REQUEST ['id'] )) {
			$id = $_REQUEST ['id'];
			$DB = M ( 'school' );
			$cnt = $DB->find ( $id );
			
			$this->assign ( 'cnt', $cnt );
			$this->assign ( 'title', C ( "WEB_NAME" ) . "修改院校信息" );
		}
		$this->display ( 'add_school' );
	}
	//添加或修改院校列表
	public function addschoolitem() {
		$DB = D ( 'school' );
		if (isset ( $_REQUEST ['id'] )) {
			if (! $DB->create ()) {
				exit ( $DB->getError () . '<a href="javascript:history.go(-1)">返回</a>' );
			}
			
			$DB->save ();

			$this->redirect ( 'listschool' );
		} else {
			if (! $DB->create ()) {
				exit ( $DB->getError () . '<a href="javascript:history.go(-1)">返回</a>' );
			}
			$DB->add ();
			$this->success ( '添加成功' );
		
		}
	}
	
	//删除院校列表
	public function delschool() {
		$id = $_REQUEST ['id'];
		$DB = M ( 'school' );
		$DB->where ( 'id in(' . $id . ')' )->delete ();
		
		unset ( $DB );
		$DB = M ( 'schoolchilditem' );
		$DB->where ( 'schoolid in(' . $id . ')' )->delete ();
	
	}
	//设置页面
	public function setting() {
		$selarr = array (0 => '所在地', 1 => '学历层次', 2 => '院校隶属', 3 => '办学类型', 4 => '院校类型', 5 => '招生计划年份', 6 => '招生计划类型',7=>'科类' );
		$this->assign ( 'title', C ( 'WEB_NAME' ) . '系统设置' );
		$this->assign ( 'selects', $selarr );
		
		//如果选择了字段设置执行以下函数
		if (isset ( $_REQUEST ['cid'] )) {
			import ( "ORG.Util.Page" );
			$listRows = 10; //每页显示条数
			$cid = $_REQUEST ['cid'];
			
			$this->assign ( 'selectname', $selarr [$cid] );
			
			$S = S ( 'setting' ); //缓存的结果
			$this->assign ( 'list', $S [$cid] );
		}
		$this->display ();
	}
	
	//保存设置
	public function savesetting() {
		$data ['type'] = $_REQUEST ['type'];
		$data ['name'] = urldecode ( $_REQUEST ['name'] );
		$S = S ( 'setting' );
		if (array_search ( $data ['name'], $S [$data ['type']] )) {
			$this->error ( '已经有此函数', true );
		}
		$DB = M ( 'schoolsetting' );
		$DB->add ( $data );
		R ( 'Admin.mycache', 'reset' ); //重置缓存
	}
	
	//删除设置
	public function deletesetting() {
		$id = $_REQUEST ['ids'];
		$DB = M ( 'schoolsetting' );
		$result = $DB->where ( 'id in(' . $id . ')' )->delete ();
		var_dump ( $result );
		var_dump ( $DB );
		R ( 'Admin.mycache', 'reset' ); //重置缓存
	

	}
	
	//专业页面
	public function speciality() {
		$spe = S ( 'speciality' );
		$spemenuarr = $this->makemenuarr ( $spe ); //生成分级菜单数组
		$this->assign ( 'sepcialitytable', $this->makespecialitymenu ( $spemenuarr, 1, 0 ) ); //生成table格式分级菜单
		$this->assign ( 'specialityselect', $this->makespecialitymenu ( $spemenuarr, 0, 0 ) ); //生成select格式分级菜单
		$this->assign ( 'title', C ( 'WEB_NAME' ) . '专业页面' );
		$this->display ();
	}
	//添加专业设置
	public function savespeciality() {
		$DB = M ( 'speciality' );
		$data ['title'] = $_REQUEST ['title'];
		$data ['pid'] = $_REQUEST ['pid'];
		$DB->add ( $data );
		R ( 'Admin.mycache', 'makespeciality' );
	}
	//删除专业设置
	public function deletespeciality() {
		$spe = S ( "speciality" );
		$id = $_REQUEST ['id'];
		$DB = M ( "speciality" );
		$ids = explode ( ',', $id );
		foreach ( $ids as $v ) {
			$idalls .= $v . ',';
			$idalls .= $this->getallsunid ( $spe, $v );		//获取当前id下的所有子级栏目ID
		}
		if ($idalls != '') {
			$idalls = substr ( $idalls, 0, - 1 );
		}
		$DB->where ( 'id in(' . $idalls . ')' )->delete ();
		//删除所有学校专业里面包含 上述已删除的专业。
		//不删除会导致冗余代码。
		$schoolspeDB = M('schoolspeciality');		//学校所有专业数据库
		$schoolspeDB->where('specialityid in('.$idalls.')')->delete();
		//更新专业的缓存
		R ( 'Admin.mycache', 'makespeciality' );
	}
	//生成专业联动形式数组
	private function makemenuarr($arr, $pid = 0) {
		foreach ( $arr as $k => $v ) {
			if ($v ['pid'] == $pid) {
				$array [$k] = $v;
				$array [$k] ['child'] = $this->makemenuarr ( $arr, $k );
			}
		}
		return $array;
	}
	//创建专业分级菜单
	private function makespecialitymenu($arr, $type = array(0=>'select',1=>'table'), $c = 0, $space = '', $splie = array(0=>'└',1=>'├')) {
		$p = 0; //开始结点
		$count = count ( $arr ); //数据总数
		

		$str = null; //内容
		foreach ( $arr as $k => $v ) {
			$p ++;
			if ($type == 0) {
				$str .= '<option value="' . $k . '">';
				if ($c != 0) {
					if ($p != $count) {
						$str .= $space . $splie [1];
					} else {
						$str .= $space . $splie [0];
					}
				}
				$str .= $v ['title'] . '</option>';
			} else if ($type == 1) {
				$str .= "<tr><td><input type='checkbox' class='ctl div" . $p . "' value='" . $k . "' ></td><td>";
				if ($c != 0) {
					if ($p != $count) {
						$str .= $space . $splie [1];
					} else {
						$str .= $space . $splie [0];
					}
				}
				$str .= $v ['title'] . '</td></tr>';
			}
			
			if ($v ['child'] != null) {
				$str .= $this->makespecialitymenu ( $v ['child'], $type, $c + 1, $space . '&nbsp;&nbsp;', $splie );
			}
		
		}
		
		return $str;
	
	}
	
	//获取专业所有子级栏目id
	private function getallsunid($arr, $pid = 0) {
		foreach ( $arr as $k => $v ) {
			if ($v ['pid'] == $pid) {
				$str .= $k . ',';
				$str .= $this->getallsunid ( $arr, $k );
			}
		}
		return $str;
	}
	
	//院校设置
	public function schoolsetting() {
		$this->assign ( 'title', C ( "WEB_NAME" ) . '院校设置' );
		if(isset($_REQUEST['schoolid']))
		{
			$type_tit_arr = array (0 => '院系设置', 1 => '招生简章', 2 => '往年录取信息', 3 => '录取结果公示', 4 => '录取规则', 5 => '体检要求', 6 => '收费项目', 7 => '奖学金设置', 8 => '食宿条件', 9 => '基础设施', 10 => '公示栏', 11 => '毕业生就业', 12 => '答考生问', 13 => '联系办法', 14 => '其他' );
			$this->assign('type',$type_tit_arr);		//操作类型
		}
			//院校列表
		$DB_school = M('school');
		$school = $DB_school->select();
		$this->assign('school',$school);
		$this->assign('selschool',$school_list[$_REQUEST['schoolid']]);		//已选择的院校
		$this->display ();
	}
	
	//院校子栏目设置 列表
	public function SCItemlist() {
		if (! isset ( $_REQUEST ['type'] ) && !isset($_REQUEST['schoolid'])) {
			exit ( '参数错误' );
		}
		$type_tit_arr = array (0 => '院系设置', 1 => '招生简章', 2 => '往年录取信息', 3 => '录取结果公示', 4 => '录取规则', 5 => '体检要求', 6 => '收费项目', 7 => '奖学金设置', 8 => '食宿条件', 9 => '基础设施', 10 => '公示栏', 11 => '毕业生就业', 12 => '答考生问', 13 => '联系办法', 14 => '其他' );
		$type = $_REQUEST['type'];
		$schoolid = $_REQUEST['schoolid'];
		if(!array_key_exists($type,$type_tit_arr))
		{
			exit('参数错误');
		}
		$title = $type_tit_arr[$type];
		$this->assign('title',C('WEB_NAME') . $title);//标题 
		$DB_school = M('school');
		$school = $DB_school->find($schoolid);
		$this->assign('school',$school);
		
		$DB_SCItem = M('schoolchilditem');
		import ( "ORG.Util.Page" );
		$listRows = 10; //每页显示的条数
		$count = $DB_SCItem->where('schoolid = ' . $schoolid . ' and type = '.$type)->count ();
		$P = new Page ( $count, $listRows );
		$show = $P->show ();
		$list = $DB_SCItem->where('schoolid = ' . $schoolid . ' and type = '.$type)->order ( 'id desc' )->limit ( $P->firstRow . ',' . $P->listRows )->select ();

		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display('schoolchilditemsetting');
	}

	
	//编辑院校子内容设置
	public function SCItemedit()
	{
	if (! isset ( $_REQUEST ['type'] )) {
			exit ( '参数错误' );
		}
		$type_tit_arr = array (0 => '院系设置', 1 => '招生简章', 2 => '往年录取信息', 3 => '录取结果公示', 4 => '录取规则', 5 => '体检要求', 6 => '收费项目', 7 => '奖学金设置', 8 => '食宿条件', 9 => '基础设施', 10 => '公示栏', 11 => '毕业生就业', 12 => '答考生问', 13 => '联系办法', 14 => '其他' );
		$type = $_REQUEST['type'];		//类型
		$schoolid = $_REQUEST['schoolid'];	//学院ID
		if(!array_key_exists($type,$type_tit_arr))
		{
			exit('参数错误');
		}
		
		$DB_school = M('school');
		$school = $DB_school->select();
		$school_list = array();
		foreach($school as $k=>$v)
		{
			$school_list[$v['id']] = $v;
		}
		$this->assign('schoollist',$school_list);			//学院列表
		$title = $school_list[$schoolid]['title'] . $type_tit_arr[$type];	//
		$this->assign('caption',$title);				//导航标题设置
		$this->assign('title',C('WEB_NAME') . $title.'_添加页面');//标题
		Vendor('ckeditor.ckeditor');	///创建FCK
		Vendor('ckfinder.ckfinder');		///创建FCK上传插件
		$editor = new CKEditor();
		$editor->config=array('width'=>500,'height'=>100,'default'=>'base','uiColor'=>'#F7F8F9');
		$editor->returnOutput=true;
		$FCK_plug = new CKFinder();
		$FCK_plug->SetupCKEditor($editor);
	
		if(isset($_REQUEST['id']))
		{
			$DB = M('schoolchilditem');
			$arr = $DB->find($_REQUEST['id']);
			$this->assign('cnt',$arr);
			$sss = $editor->editor('content',$arr['content']);
		}else 
		{
			$sss = $editor->editor('content');
		}
		
		
		$this->assign('html',$sss);
		$this->display('SCItemedit');
		
	}
	
	
	
	//新增或保存院校子栏目设置
	public function saveSCItem()
	{
		$DB = D('schoolchilditem');
		$DB->create() or die($DB->getError().   '<a href="javascript:history.go(-1)">返回</a>');
		if(isset($_REQUEST['id']))
		{
			$DB->save();			
		}else 
		{
			$DB->add();
		}
		$this->success ( '添加成功' );
	}
	
	//删除专业子栏目设置
	public function delschoolitem()
	{
		$DB = M('schoolchilditem');
		$id = $_REQUEST['id'];
		$type =  $_REQUEST['type'];
		$schoolid = $_REQUEST['schoolid'];
		$DB->where('id in('.$id.') and type='.$type.' and schoolid = '.$schoolid)->delete();
	}
	
	
	//关于我们
	public function about()
	{
		if(!isset($_REQUEST['schoolid']))
		{
			exit('参数错误');
		}	
		$DB_school = M('school');
		if(!($sch = $DB_school->find($_REQUEST['schoolid'])) )
		{
			exit('参数错误');
		}
		$this->assign('schoolname',$sch['title']);
		
		/***
		 * FCK组件
		*/
		Vendor('ckeditor.ckeditor');	///创建FCK
		Vendor('ckfinder.ckfinder');		///创建FCK上传插件
		$config =  array(
	    array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike' ),
	    array( 'Image', 'Link', 'Unlink', 'Anchor' )
	  );
		$editor = new CKEditor();
		$editor->config=array('width'=>400,'height'=>200,'default'=>'base','uiColor'=>'#F7F8F9','toolbar'=>$config);
		$editor->returnOutput=true;
		$FCK_plug = new CKFinder();
		$FCK_plug->SetupCKEditor($editor);
		$DB = M('about');
		if(($cnt =$DB->where('schoolid = '.$_REQUEST['schoolid'])->find()))	//如果有此内容，查找并更新，否则新建
		{
			$this->assign('cnt',$cnt);
			$leaderhtml = $editor->editor("leader",$cnt['leader']);
			$about = $editor->editor("about",$cnt['about']);
			$env = $editor->editor("environment",$cnt['environment']);
		}
		else
		{
			$leaderhtml = $editor->editor("leader");
			$about = $editor->editor("about");
			$env = $editor->editor("environment");
		}
		$this->assign('lea_html',$leaderhtml);
		$this->assign('abo_html',$about);
		$this->assign('env_html',$env);		
		$this->display('about');
	}
	//保存学校简介的修改
	public function editabout()
	{
		$DB = D('about');
		$DB->create();
		if(isset($_REQUEST['id']))
		{			
			$DB->save();
		}else
		{
			$DB->add();
		}
		$this->success ('更新成功');
	}
	
	//计划列表
	public function planlist()
	{
		if(!isset($_REQUEST['schoolid']))
		{
			exit('参数错误');
		}
		$DB = M('recruitingplan');
		$DB->select() ;		//TODO 先做专业模块，做完后再做这个。
		
	}
	//专业列表
	public function SCHspeciality()
	{
		if(!isset($_REQUEST['schoolid']))
		{
			exit('参数错误');
		}
		
		$schoolid = $_REQUEST['schoolid'];
		$spe = S ( 'speciality' );
		$DB = M('schoolspeciality');
		$arr = $DB->where('schoolid ='.$schoolid)->select();
		
		foreach($arr as $k=>$v)
		{
			$spe[$v['specialityid']]['in_sch'] = true;
			$spe[$v['specialityid']]['sch_id'] = $v['id'];
		}
	
		$spemenuarr = $this->makemenuarr ( $spe ); //生成分级菜单数组
		$this->assign('spe_arr',$spemenuarr);
		$this->display ('sch_speciality_list');
	}
	
	
	//添加学校专业
	public function add_sch_spe()
	{
		if(!isset($_REQUEST['schoolid']) && !isset($_REQUEST['specialityid']))
		{
			exit('参数错误');
		}
		$DB = D('schoolspeciality');
		$idarr = split(',', $_REQUEST['specialityid']);
		$schoolid = $_REQUEST['schoolid'];
		foreach($idarr as $k=>$v)
		{
			$data[$k]['specialityid']  = $v;
			$data[$k]['schoolid'] = $schoolid;
			$data[$k]['updatetime'] = time();
		}
		$DB->create($data);
		$DB->addAll($data);		
	}
	
	
	public function del_sch_spe()
	{
		if(!isset($_REQUEST['id']))
		{
			exit('参数错误');
		}
		$DB = M('schoolspeciality');
		$DB->where('id in('.$_REQUEST['id'].')')->delete();
		var_dump($DB);
	}
	
	//修改专业的内容
	public function edit_sch_spe()
	{
		if(!isset($_REQUEST['id']))
		{
			exit('参数错误');
		}
		Vendor('ckeditor.ckeditor');	///创建FCK
		Vendor('ckfinder.ckfinder');		///创建FCK上传插件
		$config =  array(
				array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike' ),
				array( 'Image', 'Link', 'Unlink', 'Anchor' )
		);
		$editor = new CKEditor();
		$editor->config=array('width'=>400,'height'=>200,'default'=>'base','uiColor'=>'#F7F8F9','toolbar'=>$config);
		$editor->returnOutput=true;
		$FCK_plug = new CKFinder();
		$FCK_plug->SetupCKEditor($editor);
		$DB = M('schoolspeciality');
		$cnt = $DB->where('id='.$_REQUEST['id'])->find();
		$cnthtml = $editor->editor('content',$cnt['content']);
		$this->assign('title',C('WEB_NAME').'修改专业内容');
		$this->assign('cnteditor',$cnthtml);
		$this->assign('cnt',$cnt);
		$this->display('schoolspeciality_cnt_edit');
	}
	
	public function sch_spe_save_edit()
	{
		if(!isset($_REQUEST['id']))
		{
			exit('参数错误');
		}
		$DB = D('schoolspeciality');
		$DB->create();
		$DB->save();
		echo "<script>window.close()</script>";
	}
	
	
	//招生计划
	public function plan()
	{
		if(!isset($_REQUEST['schoolid'])) exit('参数错误');
		$schoolid = $_REQUEST['schoolid'];
		$DB = M('recruitingplan');		//招生计划表
		$arr = $DB->where('schoolid ='.$schoolid)->select();
		$this->assign('plan_list',$arr);
		$setting = S ( "setting" ); //获取系统字段
		$years = $setting['5'];
		$this->assign('years',$years);
		$this->display('plan_list');
	}
	
	//添加年份的招生计划
	public function plan_add_years()
	{
		if(!isset($_REQUEST['schoolid']) || !isset($_REQUEST['yearid'])){exit('参数错误');}
		$DB = D('recruitingplan');
		$data['yearid'] = $_REQUEST['yearid'];
		$data['schoolid'] = $_REQUEST['schoolid'];
		$DB->create($data) or die($DB->getError(). "<a href='javascript:history.go(-1);'>返回</a>");
		$DB->add();
		$this->success('添加成功');
	}
	
	//操作某年的招生计划
	public function ctl_plan()
	{
		if(!isset($_REQUEST['pid'])) exit('参数错误');
		$pid = $_REQUEST['pid'];
		$DB_parent = M('recruitingplan');
		$parent = $DB_parent->where('id='.$pid)->find();
		
		//专业相关
		$DB_speciality_all = M('speciality');
		$__spe_all = $DB_speciality_all->select();
		$spe_alls = array();			//所有专业
		foreach($__spe_all as $v)
		{
			$spe_alls[$v['id']] = $v;
		}
		$DB_school_spiciality = M('schoolspeciality');
		$spe_school_arr = $DB_school_spiciality->where('schoolid = '.$parent['schoolid'])->select();	//所有本学院的专业
		$speciality = array();
		
		foreach($spe_school_arr as $k=>$v)
		{
			$data = $v;
			$data['spe_name'] = $spe_alls[$v['specialityid']]['title'];
			$speciality[$v['specialityid']]=$data;
		}
		$this->assign('speciality',$speciality);

		
		$setting = S('setting');//获取系统字段
		$years = $setting[5];	//年份
		$address =$setting[0];	//地址
		$plan_type = $setting[6];	//招生计划类型
		$stu_type = $setting[1];		//学历层次
		$subjecttype = $setting[7];			//科类
		
		
		$this->assign('years',$years);
		$this->assign('address',$address);
		$this->assign('plan_type',$plan_type);
		$this->assign('stu_type',$stu_type);
		$this->assign('subject',$subjecttype);
		
		
		
		
		
		
		//列表显示已经存在的数据，和分页
		$DB = M('recruitingplanattached');
		$where['pid']= array('eq',$pid);
		if(isset($_REQUEST['addid']) && $_REQUEST['addid']!='')
		{
			$where['address']=array('eq',$_REQUEST['addid']);
		}
		if(isset($_REQUEST['speid']) && $_REQUEST['speid']!='')
		{
			$where['specialityid'] = array('eq',$_REQUEST['speid']);
		}
		
		import ( "ORG.Util.Page" );
		$listRows = 10; //每页显示的条数
		$count = $DB->where($where)->count ();
		$P = new Page ( $count, $listRows );
		$show = $P->show ();
		$content = $DB->where($where)->order ( 'id desc' )->limit ( $P->firstRow . ',' . $P->listRows )->select ();
		$this->assign('content',$content);
		$this->assign('page',$show);
		$this->display('ctl_plan');
		
	}
	
	
	public function addplan()
	{
		$DB = D('recruitingplanattached');
		$DB->create();
		$DB->add();
		$this->success('添加成功');
	}
	//删除计划内容
	public function delplanitem()
	{
		if(!isset($_REQUEST['id']))
		{
			exit('参数错误');
		}
		$id = $_REQUEST['id'];
		$DB = M('recruitingplanattached');
		if($DB->where('id='.$id)->delete())
		{
			$this->success('删除成功');
		}
	}
	//管理员页面
	public function admin()
	{
		$DB = M('admin');
		$list = $DB->select();
		$this->assign('list',$list);
		$this->display('admin');
	}
	//添加管理员
	public function addadmin()
	{
		$DB = D('admin');
		$DB->create() or die($DB->getError(). "<a href='javascript:history.go(-1);'>返回</a>");
				//$DB->add();
		var_dump($DB);
		var_dump($_REQUEST);
		exit();
		$this->success('添加成功');
	}
}
?>